﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Linq;
using EOSCRM.Domain;
using EOSCRM.Util;

namespace EOSCRM.Dao
{
    public  class KyDuyetDao:BaseDao
    {
        //private readonly EventsLogDataContext _db;
        public static readonly string Connectionstring = ConfigurationManager.AppSettings[Constants.SETTINGS_CONNECTION];
        public string SessionIdentify = "_EL_USER";
        private const string _dbName = "EventsLog";
        private const string _dbNameID = "Initial Catalog";

        public KyDuyetDao()
        {
            //var connStr = ConfigurationManager.AppSettings[Constants.SETTINGS_CONNECTION];
            //var decryptString = connStr; // EncryptUtil.Decrypt(connStr);

            //var arrCS = decryptString.Split(';');

            //for (var i = 0; i < arrCS.Length; i++)
            //{
            //    var keyPair = arrCS[i].Split('=');
            //    if (keyPair.Count() == 2 && keyPair[0] == _dbNameID)
            //        // EOSDN_EventsLog
            //        arrCS[i] = string.Format("{0}={1}_{2}", keyPair[0], keyPair[1], _dbName);
            //}
            //ConnectionString = String.Join(";", arrCS);
            var decryptString = Connectionstring; // EncryptUtil.Decrypt(Connectionstring);

            // userLog = HttpContext.Current.Session["_EL_USER"] as UserLog;
            _db = new EOSCRMDataContext(decryptString);
            //_db = new EventsLogDataContext(ConnectionString);
        }
        
        public LUUVET_KYDUYET Get(int macb)
        {
            return _db.LUUVET_KYDUYETs.SingleOrDefault(p => p.ID.Equals(macb));
        }

        public List<LUUVET_KYDUYET> Search(string key)
        {
            return _db.LUUVET_KYDUYETs.Where(p => p.MADON.Equals(key)).ToList();
        }

        public Message Insert(LUUVET_KYDUYET objUi)
        {
            Message msg;
            try
            {

                _db.Connection.Open();

                _db.LUUVET_KYDUYETs.InsertOnSubmit(objUi);
                _db.SubmitChanges();

                // commit

                // success message
                msg = new Message(MessageConstants.I_CREATE_SUCCEED, MessageType.Info, "");
            }
            catch (Exception ex)
            {
                // rollback transaction

                msg = ExceptionHandler.HandleInsertException(ex, "", "");
            }
            return msg;
        }
    }
}